<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    
    <title>V: Templates 模板机制 | Go Learning</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <meta name="generator" content="Hugo 0.74.3" />
    
    
      <META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"> 
    
    
      <link href="/dist/css/app.4fc0b62e4b82c997bb0041217cd6b979.css" rel="stylesheet">
    
    
        
        
          
            
    
    
    <link href="/css/main-bundle.css" rel="stylesheet">
    

    
      
<link rel="shortcut icon" href="/assets/micro_xs.png" type="image/x-icon" />

 

    
    
    <meta property="og:title" content="V: Templates 模板机制" />
<meta property="og:description" content="坚果的 Hugo 教程" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://jimbowhy.gitee.io/tutorials/ch05a-hugo-templates/" />
<meta property="article:published_time" content="2020-08-06T20:14:08-04:00" />
<meta property="article:modified_time" content="2020-08-06T20:14:08-04:00" />
<meta itemprop="name" content="V: Templates 模板机制">
<meta itemprop="description" content="坚果的 Hugo 教程">
<meta itemprop="datePublished" content="2020-08-06T20:14:08-04:00" />
<meta itemprop="dateModified" content="2020-08-06T20:14:08-04:00" />
<meta itemprop="wordCount" content="1636">



<meta itemprop="keywords" content="hugo,menu," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="V: Templates 模板机制"/>
<meta name="twitter:description" content="坚果的 Hugo 教程"/>

	
  </head>

  <body class="ma0 avenir bg-near-white">

    
   
  






  <header>
    <div class="bg-black">
      

<nav class="pv3 ph3 ph4-ns" role="navigation">
  <div class="flex-l justify-between items-center center">

    <a href="/" class="logo f3 fw2 hover-white no-underline white-90 dib">
      
        <img src="/assets/micro_xs.png" class="w100 mw5-ns" alt="Go Learning" />
      
    </a>

    <div class="flex-l items-center">
      

      
        <ul class="pl0 mr3">
           

          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline  white-90" href="/tutorials/" title="Hugo 教程 page">
              Hugo 教程
            </a>
          </li>       

          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline  white-90" href="/about/" title="关于 page">
              关于
            </a>
          </li>     

          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline  white-90" href="/post/" title="小说 page">
              小说
            </a>
          </li>     

          <li class="list f5 f4-ns fw4 dib pr3">
            <a class="hover-white no-underline  white-90" href="/contact/" title="联络 page">
              联络
            </a>
          </li>  
        </ul>
      
      







<a href="https://www.linkedin.com/in/jimboyeah/?zh" target="_blank" class="link-transition linkedin link dib z-999 pt3 pt0-l mr1" title="LinkedIn link" rel="noopener" aria-label="follow on LinkedIn——Opens in a new window">
  <svg  height="32px"  style="enable-background:new 0 0 65 65;" version="1.1" viewBox="0 0 65 65" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <path d="M50.837,48.137V36.425c0-6.275-3.35-9.195-7.816-9.195  c-3.604,0-5.219,1.983-6.119,3.374V27.71h-6.79c0.09,1.917,0,20.427,0,20.427h6.79V36.729c0-0.609,0.044-1.219,0.224-1.655  c0.49-1.22,1.607-2.483,3.482-2.483c2.458,0,3.44,1.873,3.44,4.618v10.929H50.837z M22.959,24.922c2.367,0,3.842-1.57,3.842-3.531  c-0.044-2.003-1.475-3.528-3.797-3.528s-3.841,1.524-3.841,3.528c0,1.961,1.474,3.531,3.753,3.531H22.959z M34,64  C17.432,64,4,50.568,4,34C4,17.431,17.432,4,34,4s30,13.431,30,30C64,50.568,50.568,64,34,64z M26.354,48.137V27.71h-6.789v20.427  H26.354z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>

<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000" width="8px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>
</span></a>


<a href="https://github.com/jimboyeah/jimboyeah.github.io" target="_blank" class="link-transition github link dib z-999 pt3 pt0-l mr1" title="Github link" rel="noopener" aria-label="follow on Github——Opens in a new window">
  <svg  height="32px"  style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>

<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000" width="8px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>
</span></a>








    </div>

  </div>
</nav>

    </div>
  </header>




    

    <main class="pb5" role="main">
      
  
  
  <article class="flex-l flex-wrap justify-between mw8 center ph3">
    <header class="mt4 w-100">
      <aside class="instapaper_ignoref b helvetica tracked">
          
        HUGO 教程
      </aside>
      




  <div id="sharing" class="mt3">

    
    <a href="https://www.facebook.com/sharer.php?u=https://jimbowhy.gitee.io/tutorials/ch05a-hugo-templates/" class="facebook no-underline" aria-label="share on Facebook">
      <svg height="32px"  style="enable-background:new 0 0 67 67;" version="1.1" viewBox="0 0 67 67" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M28.765,50.32h6.744V33.998h4.499l0.596-5.624h-5.095  l0.007-2.816c0-1.466,0.14-2.253,2.244-2.253h2.812V17.68h-4.5c-5.405,0-7.307,2.729-7.307,7.317v3.377h-3.369v5.625h3.369V50.32z   M33,64C16.432,64,3,50.569,3,34S16.432,4,33,4s30,13.431,30,30S49.568,64,33,64z" style="fill-rule:evenodd;clip-rule:evenodd;"/></svg>

    </a>

    
    
    <a href="https://twitter.com/share?url=https://jimbowhy.gitee.io/tutorials/ch05a-hugo-templates/&amp;text=V:%20Templates%20%e6%a8%a1%e6%9d%bf%e6%9c%ba%e5%88%b6" class="twitter no-underline" aria-label="share on Twitter">
      <svg height="32px"  style="enable-background:new 0 0 67 67;" version="1.1" viewBox="0 0 67 67" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M37.167,22.283c-2.619,0.953-4.274,3.411-4.086,6.101  l0.063,1.038l-1.048-0.127c-3.813-0.487-7.145-2.139-9.974-4.915l-1.383-1.377l-0.356,1.017c-0.754,2.267-0.272,4.661,1.299,6.271  c0.838,0.89,0.649,1.017-0.796,0.487c-0.503-0.169-0.943-0.296-0.985-0.233c-0.146,0.149,0.356,2.076,0.754,2.839  c0.545,1.06,1.655,2.097,2.871,2.712l1.027,0.487l-1.215,0.021c-1.173,0-1.215,0.021-1.089,0.467  c0.419,1.377,2.074,2.839,3.918,3.475l1.299,0.444l-1.131,0.678c-1.676,0.976-3.646,1.526-5.616,1.568  C19.775,43.256,19,43.341,19,43.405c0,0.211,2.557,1.397,4.044,1.864c4.463,1.377,9.765,0.783,13.746-1.568  c2.829-1.673,5.657-5,6.978-8.221c0.713-1.716,1.425-4.851,1.425-6.354c0-0.975,0.063-1.102,1.236-2.267  c0.692-0.678,1.341-1.419,1.467-1.631c0.21-0.403,0.188-0.403-0.88-0.043c-1.781,0.636-2.033,0.551-1.152-0.402  c0.649-0.678,1.425-1.907,1.425-2.267c0-0.063-0.314,0.042-0.671,0.233c-0.377,0.212-1.215,0.53-1.844,0.72l-1.131,0.361l-1.027-0.7  c-0.566-0.381-1.361-0.805-1.781-0.932C39.766,21.902,38.131,21.944,37.167,22.283z M33,64C16.432,64,3,50.569,3,34S16.432,4,33,4  s30,13.431,30,30S49.568,64,33,64z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/></svg>

    </a>

    
    <a href="https://www.linkedin.com/shareArticle?mini=true&amp;url=https://jimbowhy.gitee.io/tutorials/ch05a-hugo-templates/&amp;title=V:%20Templates%20%e6%a8%a1%e6%9d%bf%e6%9c%ba%e5%88%b6" class="linkedin no-underline" aria-label="share on LinkedIn">
      <svg  height="32px"  style="enable-background:new 0 0 65 65;" version="1.1" viewBox="0 0 65 65" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <path d="M50.837,48.137V36.425c0-6.275-3.35-9.195-7.816-9.195  c-3.604,0-5.219,1.983-6.119,3.374V27.71h-6.79c0.09,1.917,0,20.427,0,20.427h6.79V36.729c0-0.609,0.044-1.219,0.224-1.655  c0.49-1.22,1.607-2.483,3.482-2.483c2.458,0,3.44,1.873,3.44,4.618v10.929H50.837z M22.959,24.922c2.367,0,3.842-1.57,3.842-3.531  c-0.044-2.003-1.475-3.528-3.797-3.528s-3.841,1.524-3.841,3.528c0,1.961,1.474,3.531,3.753,3.531H22.959z M34,64  C17.432,64,4,50.568,4,34C4,17.431,17.432,4,34,4s30,13.431,30,30C64,50.568,50.568,64,34,64z M26.354,48.137V27.71h-6.789v20.427  H26.354z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>

    </a>
  </div>


      <h1 class="f3 athelas mt3 mb1">V: Templates 模板机制</h1>
      
      
      <time class="f6 mv4 dib tracked" datetime="2020-08-06T20:14:08-04:00">August 6, 2020</time>

      
      
    </header>

    <div class="nested-copy-line-height lh-copy serif f5 nested-links nested-img mid-gray pr4-l w-two-thirds-l">
       
      <a title="Prev Post" class="prev fl" href="https://jimbowhy.gitee.io/tutorials/ch06-multilingual/">VI: Multilingual 多语言支持</a>
      
       
      <a title="Next Post" class="next fr" href="https://jimbowhy.gitee.io/tutorials/ch05b-hugo-templates/">V: Templates 其它模板</a>
      
      <p class="cf"></p>

      <p>目录：</p>
<div class="contents"><a href="#模板加载流程">## 模板加载流程</a><br><a href="#templates-模板语法">## Templates 模板语法</a><br><a href="#base-template-页面骨架模板">## Base Template 页面骨架模板</a><br></div>
<h2 id="模板加载流程">模板加载流程</h2>
<p>这里对 Hugo 静态站点框架作一个简要的流程分析，主要是分析数据流向。</p>
<p>首先，Hugo 是基于 Go 语言实现的静态站点生成器，并且利用了 GO html/template 模板、Go Modules 等多项技术。</p>
<p>静态站点的数据内容主要来源有四个：</p>
<ul>
<li>一是 Markdown 文件，每个 MD 文件顶部可以设置参数，这个区域叫做扉页 Front Matter，文件后面的是内容；</li>
<li>二是 config.toml 站点的配置文件；</li>
<li>三是 data 目录下的数据文件</li>
<li>最后是通过 Ajax 技术，还可以在页面中请求外部服务器的数据服务，比如表单数据、评论数据等等。</li>
</ul>
<p>这些就是 Hugo 静态站点工程中的主要数据来源。</p>
<p>有了数据，接下来就是将数据套用到模板中，而模板与页面类型密切相关。Hugo 的页面是一个大概念，比如浏览看到的页面就是一般意义 Page 概念，Hugo 还细分成 Home Page 页面、RSS 页面、单页 Single、列表页 List、分类页 Taxonomy、站点地图页 Sitemap 等。</p>
<p>工程目录下的 <strong>content</strong> 就是存放各种页面内容文件的地方，参考下面的内容目录结构与 URL 对应关系参考：</p>
<pre><code>.
└─ content
   ├─ index.md       &lt;-- https://example.com/
   ├─ about
   |  └─ index.md    &lt;-- https://example.com/about/
   ├─ posts
   |  ├─ _index.md   &lt;-- https://example.com/posts/
   |  ├─ index.md    &lt;-- https://example.com/posts/
   |  ├─ firstpost.md&lt;-- https://example.com/posts/firstpost/
   |  ├─ happy
   |  |  └─ ness.md  &lt;-- https://example.com/posts/happy/ness/
   |  └─ secondpost.md &lt;-- https://example.com/posts/secondpost/
   └─ quote
      ├─ first.md    &lt;-- https://example.com/quote/first/
      └─ second.md   &lt;-- https://example.com/quote/second/
</code></pre>
<p>以上的文件中，<strong>index.md</strong> 或 <strong>_index.md</strong> 这样的文件称为列表内容页面，或称为主页，顶级的 <strong>index.md</strong> 称为站点主页。一级子目录 about、posts、quote 称为分区 Sessions，每个区对应一个列表页面和任意的内容页，它们和 <strong>layouts</strong> 目录下的模板匹配关系如下：</p>
<pre><code>├── _default
│   ├── baseof.html
│   ├── list.html
│   └── single.html
└── index.html
</code></pre>
<p>这些不同的页面类型会匹配到不同的模板文件，而模板文件根据将获取到的数据进行渲染成页面，经过 Hugo 转译处理后生成 <strong>public</strong> 目录下的静态站点文件。</p>
<p>以请求首页为例，先看看模板匹配及加载流程图：</p>
<p><img src="/assets/hugo-templates.jpg" alt="hugo-templates.jpg"></p>
<!-- raw HTML omitted -->
<!-- raw HTML omitted -->
<p>Hugo 提供了丰富的模板匹配候选项，主要提供的匹配规则有三点：</p>
<ul>
<li>目录结构匹配，即 Sections 匹配，比如，内容子目录 posts 下的页面会匹配 <strong>layouts</strong> 下同名子目录的模板；</li>
<li>默认目录匹配，模板目录下的 <strong>_default</strong> 子目录是存放最终候选模板文件的目录；</li>
<li>页面类型匹配，主页、列表页、分类页等都会匹配列表模板 <strong>list.html</strong>，主页还会优先匹配 <strong>index.html</strong> 模板；</li>
</ul>
<p>并且，工程根目录下和主题目录下的 <strong>layouts</strong> 具有同样组织结构，Hugo 首先会在项目根目录下的 <strong>layouts</strong> 中搜索要匹配的模板，如果在 <strong>layouts</strong> 顶层找不到，就进入 <strong>_default</strong> 子目录下找，如果还没找不到，就到主题目录下去找继续。</p>
<p>这里提一下 <strong>baseof.html</strong> 这个模板，它是骨架模板，在装入这个骨架模板之后，才会继装入其它模板。在 <strong>baseof.html</strong> 中可以定义 block 内容块，只提供简单的默认内容，然后由其它模板用 define 关键字去实现这个内容块的具体内容：</p>










<div class="code relative" id="demo.html"><div class="filename san-serif f6 dib lh-solid pl2 pv2">demo.html</div>
        <button class="needs-js copy bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button">
        </button>
        
    
    <div class="code-copy-content nt3" >
        <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go-html-template" data-lang="go-html-template"> <span style="color:#75715e">&lt;!DOCTYPE html&gt;</span>
    &lt;<span style="color:#f92672">html</span> <span style="color:#a6e22e">lang</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;en&#34;</span>&gt;
      &lt;<span style="color:#f92672">head</span>&gt;
        &lt;<span style="color:#f92672">title</span>&gt;<span style="color:#75715e">{{</span> <span style="color:#a6e22e">.Site.Title</span> <span style="color:#75715e">}}</span>&lt;/<span style="color:#f92672">title</span>&gt;
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;favicon&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;head&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">partial</span> <span style="color:#e6db74">&#34;head-additions.html&#34;</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
      &lt;/<span style="color:#f92672">head</span>&gt;
      &lt;<span style="color:#f92672">body</span>&gt;
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;header&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">partial</span> <span style="color:#e6db74">&#34;site-header.html&#34;</span> <span style="color:#a6e22e">.</span><span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;main&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">printf</span> <span style="color:#e6db74">&#34;%#v&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;footer&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">partialCached</span> <span style="color:#e6db74">&#34;site-footer.html&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;scripts&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">partialCached</span> <span style="color:#e6db74">&#34;site-scripts.html&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
      &lt;/<span style="color:#f92672">body</span>&gt;
    &lt;/<span style="color:#f92672">html</span>&gt;
</code></pre></div>
    </div>

</div>

<p>在模板中，可以使用 <strong>{{ printf &ldquo;%#v&rdquo; . }}</strong> 打印当前上下文对象的详细信息，是一个调试方法。</p>
<p>另外，404 处理中，一般 Web 服务器都会有一个默认 404 页面地址，通常配置为站点根目录下的 404.html 文件。如 GitHub Pages 的 404 页面是自动处理的，Apache 一般在 .htaccess 配置为 /404.html，还有 Nginx 也是。</p>
<p>注意，在模板匹配规则中，需要搞清楚 TYPE、VIEW 概念，它们是模板匹配规则的基本数据：</p>
<ul>
<li>页面类型 TYPE 决定了从什么目录中搜索模板文件；</li>
<li>视图类型 VIEW 决定了加载什么名字的模板；</li>
</ul>
<p>比如列表、主页、分类页面加载的是 list.html、index.html、taxonomy.html、terms.html 这种模板文件。而对于一般的 Page 页面类型，加载的是 single.html 这样的模板文件。</p>
<p>如下目录结构：</p>
<pre><code>content
└── blog        &lt;-- Section, because first-level dir under content/
    ├── posts
    │   ├── mypost.md       &lt;--- Page
    │   └── kittens         &lt;-- Section, because contains _index.md
    │       └── _index.md
    └── tech                &lt;-- Section, because contains _index.md
        └── _index.md
</code></pre>
<p>所有页面 Page 对象都有一个 .Kind 属性变量，它代表的是页面类型，即模板匹配中需要用到的 TYPE 类型，如下：</p>


<table class="table is-striped is-hoverable">
<thead>
<tr>
<th>Kind</th>
<th>说明</th>
<th>生成对应页面</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>home</code></td>
<td>加载首页</td>
<td><code>/index.html</code></td>
</tr>
<tr>
<td><code>page</code></td>
<td>加载定页面</td>
<td><code>my-post</code> page (<code>/posts/my-post/index.html</code>)</td>
</tr>
<tr>
<td><code>section</code></td>
<td>加载分区页</td>
<td><code>posts</code> section (<code>/posts/index.html</code>)</td>
</tr>
<tr>
<td><code>taxonomy</code></td>
<td>加载分类页</td>
<td><code>tags</code> taxonomy (<code>/tags/index.html</code>)</td>
</tr>
<tr>
<td><code>term</code></td>
<td>加载分类术语页</td>
<td>term <code>awesome</code> in <code>tags</code> taxonomy (<code>/tags/awesome/index.html</code>)</td>
</tr>
</tbody>
</table>


<p>TYPE 指页面的类型，首先，明确 <strong>content</strong> 一级子目录的类型就是 section，其次，包含 <strong>_index.md</strong> 的目录也是 section 类型。</p>
<p>TYPE 还可以在内容文件中指定：</p>
<pre><code>---
title: Front Matter
type: page
---
</code></pre>
<p>不在内容文件中指定页面类型的情况下，会默认自动从目录继承页面类型属性。</p>
<p>前面简单理了一遍 baseof.html 模板的搜索定位，其实，根据不同的页面类型，骨架模板还可以有更多的匹配选择：</p>
<ol>
<li><code>/layouts/section/&lt;TYPE&gt;-baseof.html</code></li>
<li><code>/themes/&lt;THEME&gt;/layouts/section/&lt;TYPE&gt;-baseof.html</code></li>
<li><code>/layouts/&lt;TYPE&gt;/baseof.html</code></li>
<li><code>/themes/&lt;THEME&gt;/layouts/&lt;TYPE&gt;/baseof.html</code></li>
<li><code>/layouts/section/baseof.html</code></li>
<li><code>/themes/&lt;THEME&gt;/layouts/section/baseof.html</code></li>
<li><code>/layouts/_default/&lt;TYPE&gt;-baseof.html</code></li>
<li><code>/themes/&lt;THEME&gt;/layouts/_default/&lt;TYPE&gt;-baseof.html</code></li>
<li><code>/layouts/_default/baseof.html</code></li>
<li><code>/themes/&lt;THEME&gt;/layouts/_default/baseof.html</code></li>
</ol>
<p>类型占位符号 TYPE 根据页面指定的类型改变，默认行为类型是 session，假设有个 section 名称即目录名是 posts，Hugo 会按以下顺序搜索其骨架模板：</p>
<ol>
<li><code>/layouts/section/posts-baseof.html</code></li>
<li><code>/themes/mytheme/layouts/section/posts-baseof.html</code></li>
<li><code>/layouts/posts/baseof.html</code></li>
<li><code>/themes/mytheme/layouts/posts/baseof.html</code></li>
<li><code>/layouts/section/baseof.html</code></li>
<li><code>/themes/mytheme/layouts/section/baseof.html</code></li>
<li><code>/layouts/_default/posts-baseof.html</code></li>
<li><code>/themes/mytheme/layouts/_default/posts-baseof.html</code></li>
<li><code>/layouts/_default/baseof.html</code></li>
<li><code>/themes/mytheme/layouts/_default/baseof.html</code></li>
</ol>
<p>对于内容页面，适用以下模板定位规则：</p>
<ol>
<li><code>/layouts/&lt;TYPE&gt;/&lt;VIEW&gt;.html</code></li>
<li><code>/layouts/_default/&lt;VIEW&gt;.html</code></li>
<li><code>/themes/&lt;THEME&gt;/layouts/&lt;TYPE&gt;/&lt;VIEW&gt;.html</code></li>
<li><code>/themes/&lt;THEME&gt;/layouts/_default/&lt;VIEW&gt;.html</code></li>
</ol>
<p>即，加载名称为 posts 的 section 目录下的内容页面时，会搜索以下这样的模板文件，它们分别对应列表模板和单页面模板：</p>
<ul>
<li><strong>/layout/posts/posts.html</strong></li>
<li><strong>/layout/posts/single.html</strong></li>
</ul>
<p>而，默认的类型是 page，所以加载默认类型页面时，会搜索以下这些模板文件，包括加载主页：</p>
<ul>
<li><strong>/layout/page/page.html</strong></li>
<li><strong>/layout/page/single.html</strong></li>
</ul>
<p>这些规则可以参考文档中的 docs.json 数据文件，里面有完整的参考数据。</p>
<p>总结一下，学习使用模板一是为了解内容文件夹下的内容如何通过模板文件呈现为页面。另一方面是解决，如何为指定的一个内容文件定义模板。比如，有一个内容文件 <strong>/about/slideshow.md</strong> 想要做成幻灯片展示页面，与其它页面区别开来。</p>
<p>如果是 <strong>/about/index.md</strong> 那么，可以直接定义 <strong>/_default/about.html</strong> 实现，从另一个角度看，可以将 slideshow.md 改成其它目录下的 <strong>index.md</strong> 就可以实现要求的模板指定要求。</p>
<p>又或者，在文件的 Front Matter 区指定 type 类型，再接着建立 <strong>layouts</strong> 下同名的子目录，并建立一个 <strong>single.html</strong> 模板，这样做意味可以完全构造整个 HTML 页面。</p>
<h2 id="templates-模板语法">Templates 模板语法</h2>
<ul>
<li><a href="https://godoc.org/html/template">Go html/template 模板文档</a></li>
<li><a href="https://godoc.org/text/template">Go text/template 模板文档</a></li>
<li><a href="https://themes.gohugo.io/">Hugo 主题</a></li>
<li><a href="https://gohugo.io/templates/introduction/">Hugo 模板的基本语法</a></li>
</ul>
<p>Hugo 直接使用了 Golang 的模板语法，表达能力很强大，配合 Hugo 预定义变量或自定义变量实现非常强大的静态站点功能，语法参考官方文档 templates introduction.md 文件。</p>
<p>关于 Hugo 模板的使用，参考模板介绍文档 introduction.md，其它模板类型文档如下：</p>
<pre><code>| alternatives.md   | lists.md                 | robots.md                | template-debugging.md |
| base.md           | lookup-order.md          | rss.md                   | views.md              |
| data-templates.md | menu-templates.md        | section-templates.md     |                       |
| files.md          | ordering-and-grouping.md | shortcode-templates.md   |                       |
| homepage.md       | output-formats.md        | single-page-templates.md |                       |
| internal.md       | pagination.md            | sitemap-template.md      |                       |
| introduction.md   | partials.md              | taxonomy-templates.md    |                       |
</code></pre>
<p>基本上，一套简单的模板下来，就有好几个文件：</p>
<ul>
<li><strong>layouts/_default/baseof.html</strong> 页面骨架模板 Base Template，包含 HTML 的 Head、Body 或者页面基本布局结构；</li>
<li><strong>layouts/_default/list.html</strong> 与 <strong>_index.md</strong> 等列表页面对应的列表模板，包括首页、分类页面、分类术语也属性列表；</li>
<li><strong>layouts/_default/page.html</strong> 与 page 目录对应的 session 模板；</li>
<li><strong>layouts/_default/single.html</strong> 与 md 内容对应的单面面模板；</li>
<li><strong>layouts/_default/taxonomy.html</strong> 分类列表页面模板；</li>
<li><strong>layouts/_default/terms.html</strong> 分类术语页面模板；</li>
</ul>
<p>所有模板文件在加载时，都涉及到模板文件的定位这一步骤，具体可以查阅 lookup-order.md 文档，或者直接翻 docs.json 数据，找里的 Template Lookup Order。</p>
<p>所有页面 Page 对象都有一个 .Kind 属性变量，查找模板的规则与它密切相关。</p>
<p>部分页面对象变量参考如下：</p>


<table class="table is-striped is-hoverable">
<thead>
<tr>
<th>Kind</th>
<th>说明</th>
<th>例子</th>
<th>默认输出格式</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>home</code></td>
<td>加载首页</td>
<td><code>/index.html</code></td>
<td>HTML, RSS</td>
</tr>
<tr>
<td><code>page</code></td>
<td>加载页面</td>
<td><code>my-post</code> page (<code>/posts/my-post/index.html</code>)</td>
<td>HTML</td>
</tr>
<tr>
<td><code>section</code></td>
<td>加载分区类型</td>
<td><code>posts</code> section (<code>/posts/index.html</code>)</td>
<td>HTML, RSS</td>
</tr>
<tr>
<td><code>taxonomy</code></td>
<td>加载分类页</td>
<td><code>tags</code> taxonomy (<code>/tags/index.html</code>)</td>
<td>HTML, RSS</td>
</tr>
<tr>
<td><code>term</code></td>
<td>加载分类术语页</td>
<td>term <code>awesome</code> in <code>tags</code> taxonomy (<code>/tags/awesome/index.html</code>)</td>
<td>HTML, RSS</td>
</tr>
</tbody>
</table>


<p>除了 .King 属性，还有以下相关设置：</p>
<ul>
<li>
<p>内容文件扉页设置的 Layout 属性；</p>
</li>
<li>
<p>输出内容格式设置 Output，参考 output-formats.md 文档，<code>name</code> (e.g. <code>rss</code>, <code>amp</code>, <code>html</code>) and a <code>suffix</code> (e.g. <code>xml</code>, <code>html</code>). We prefer matches with both (e.g. <code>index.amp.html</code>, but look for less specific templates.</p>
</li>
<li>
<p>语言设置 Language，比如设置 language: &ldquo;fr&rdquo;, 那么<code>index.fr.amp.html</code> 优先于 <code>index.amp.html</code> 被选中，但 <code>index.fr.html</code> 会作为前两个模板的备选，只有它们缺失时有效。</p>
</li>
<li>
<p>页面扉页数据设置 Type 类型，如 <strong>type: &ldquo;blog&rdquo;</strong>，默认值是 &ldquo;page&rdquo;。</p>
</li>
<li>
<p>Section 类型 <code>section</code>, <code>taxonomy</code>, <code>term</code> 等类型。</p>
</li>
</ul>
<p>以下几个模板是最基本的要求，主题目录都会设置它们：</p>
<pre><code>├── _default
│   ├── baseof.html
│   ├── list.html
│   └── single.html
└── index.html
</code></pre>
<p>Hugo 使用的是 Go 语言自带的模板引擎，有 Go 语言基础理解起来就更容易。模板的标签为 <strong>{{}}</strong>, 其中包含的内容叫动作 Action，动作分为两种类型：</p>
<ul>
<li>数据求值</li>
<li>控制结构</li>
</ul>
<p>求值的结果会直接输出到模板中, 控制结构主要包含条件、循环、函数调用等。</p>
<p>列如，以下这段 Go 代码演示了其内置模板的使用：</p>








<div class="code relative" id="demo.go"><div class="filename san-serif f6 dib lh-solid pl2 pv2">demo.go</div>
        <button class="needs-js copy bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button">
        </button>
        
    
    <div class="code-copy-content nt3" >
        <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go">
    <span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>

    <span style="color:#f92672">import</span> (
        <span style="color:#e6db74">&#34;text/template&#34;</span>
        <span style="color:#e6db74">&#34;os&#34;</span>
    )

    <span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
        <span style="color:#a6e22e">v</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">struct</span>{<span style="color:#a6e22e">A</span>,<span style="color:#a6e22e">B</span> <span style="color:#66d9ef">string</span>}{ <span style="color:#e6db74">&#34;foo&#34;</span>, <span style="color:#e6db74">&#34;bar&#34;</span> }

        <span style="color:#a6e22e">tmpl</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">template</span>.<span style="color:#a6e22e">New</span>(<span style="color:#e6db74">&#34;foo&#34;</span>).<span style="color:#a6e22e">Parse</span>(<span style="color:#e6db74">`</span><span style="color:#75715e">{{</span><span style="color:#a6e22e">define</span> <span style="color:#e6db74">&#34;T&#34;</span><span style="color:#75715e">}}</span><span style="color:#e6db74">Hello, </span><span style="color:#75715e">{{</span><span style="color:#a6e22e">.A</span><span style="color:#75715e">}}</span><span style="color:#e6db74"> and </span><span style="color:#75715e">{{</span><span style="color:#a6e22e">.B</span><span style="color:#75715e">}}</span><span style="color:#e6db74">!</span><span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span><span style="color:#e6db74">`</span>)
        <span style="color:#a6e22e">_</span> = <span style="color:#a6e22e">tmpl</span>.<span style="color:#a6e22e">ExecuteTemplate</span>(<span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">Stdout</span>, <span style="color:#e6db74">&#34;T&#34;</span>, <span style="color:#a6e22e">v</span>)

        <span style="color:#a6e22e">t2</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">template</span>.<span style="color:#a6e22e">New</span>(<span style="color:#e6db74">&#34;foo&#34;</span>).<span style="color:#a6e22e">Parse</span>(<span style="color:#e6db74">`</span><span style="color:#75715e">{{</span><span style="color:#a6e22e">define</span> <span style="color:#e6db74">&#34;T&#34;</span><span style="color:#75715e">}}</span><span style="color:#e6db74">Hello, </span><span style="color:#75715e">{{</span><span style="color:#a6e22e">.</span><span style="color:#75715e">}}</span><span style="color:#e6db74">!</span><span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span><span style="color:#e6db74">`</span>)
        <span style="color:#a6e22e">_</span> = <span style="color:#a6e22e">t2</span>.<span style="color:#a6e22e">ExecuteTemplate</span>(<span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">Stdout</span>, <span style="color:#e6db74">&#34;T&#34;</span>, <span style="color:#e6db74">&#34;&lt;script&gt;alert(&#39;you have been pwned&#39;)&lt;/script&gt;&#34;</span>)
    }

    <span style="color:#75715e">// Hello, foo and bar!Hello, &lt;script&gt;alert(&#39;you have been pwned&#39;)&lt;/script&gt;!
</span></code></pre></div>
    </div>

</div>

<p>在这段代码里，模板部分用反引号包括：</p>
<pre><code>{{define &quot;T&quot;}}Hello, {{.}}!{{end}}
</code></pre>
<p>点 <code>.</code> 代表传递给模板的数据，表示模板当前的上下文对象，这个数据可以 Go 语言中的任何类型，如字符串、数组、结构体等，一些模板用法参考：</p>










<div class="code relative" id="demo.html"><div class="filename san-serif f6 dib lh-solid pl2 pv2">demo.html</div>
        <button class="needs-js copy bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button">
        </button>
        
    
    <div class="code-copy-content nt3" >
        <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go-html-template" data-lang="go-html-template">
    // 注解
    <span style="color:#75715e">{{</span><span style="color:#75715e">/* comment */</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span><span style="color:#960050;background-color:#1e0010">&lt;</span> <span style="color:#a6e22e">figure</span> <span style="color:#a6e22e">src</span><span style="color:#960050;background-color:#1e0010">=</span><span style="color:#e6db74">&#34;/media/spf13.jpg&#34;</span> <span style="color:#a6e22e">title</span><span style="color:#960050;background-color:#1e0010">=</span><span style="color:#e6db74">&#34;Steve Francia&#34;</span> <span style="color:#960050;background-color:#1e0010">&gt;</span><span style="color:#75715e">}}</span>

    // 清除 pipeline 前后的空格
    <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">pipeline</span> <span style="color:#75715e">-}}</span>

    // 清除 pipeline 前面的空格
    <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">pipeline</span> <span style="color:#75715e">}}</span>

    // 清除 pipeline 后面的空格
    <span style="color:#75715e">{{</span> <span style="color:#a6e22e">pipeline</span> <span style="color:#75715e">-}}</span>

    // 变量名赋值
    <span style="color:#75715e">{{</span><span style="color:#a6e22e">$var</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;value&#34;</span><span style="color:#75715e">}}</span>

    // 条件判断流程
    // 下面这些情况 pipeline 的值为空, false, 0, nil 指针或接口, 长度为 0 的数组、切片、map 和字符串，执行 T0
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">if</span> <span style="color:#a6e22e">pipeline</span><span style="color:#75715e">}}</span> T1 <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span> 
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">if</span> <span style="color:#a6e22e">pipeline</span><span style="color:#75715e">}}</span> T1 <span style="color:#75715e">{{</span><span style="color:#66d9ef">else</span><span style="color:#75715e">}}</span> T0 <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">if</span> <span style="color:#a6e22e">pipeline</span><span style="color:#75715e">}}</span> T1 <span style="color:#75715e">{{</span><span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">pipeline</span><span style="color:#75715e">}}</span> T0 <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>

    // 遍历 pipeline 必须是数组, 切片, map, channel，在 T1 中上下文就是当前访问到的元素
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">range</span> <span style="color:#a6e22e">pipeline</span><span style="color:#75715e">}}</span> T1 <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>

    // with 设置上下文值为 pipeline
    // 如果 pipeline 的值为空，点`.`的值不受影响，输出T1，否则点`.`的值设置成 pipeline 的值, 输出T0
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">with</span> <span style="color:#a6e22e">pipeline</span><span style="color:#75715e">}}</span> T1 <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">with</span> <span style="color:#a6e22e">pipeline</span><span style="color:#75715e">}}</span> T1 <span style="color:#75715e">{{</span><span style="color:#66d9ef">else</span><span style="color:#75715e">}}</span> T0 <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>

    // 使用 define 定义一个特定名称的模板
    <span style="color:#75715e">{{</span><span style="color:#a6e22e">define</span> <span style="color:#e6db74">&#34;name&#34;</span><span style="color:#75715e">}}</span> T1 <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>

    // 使用 template 引入指定名称的模板, 不传入任何数据
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">template</span> <span style="color:#e6db74">&#34;name&#34;</span><span style="color:#75715e">}}</span>

    // 引入指定名称的模板, 设置模板上下文值为 pipeline 的值
    <span style="color:#75715e">{{</span><span style="color:#66d9ef">template</span> <span style="color:#e6db74">&#34;name&#34;</span> <span style="color:#a6e22e">pipeline</span><span style="color:#75715e">}}</span>

    // block 定义特定名称的模板区块，并在当前位置引入该名称的模板
    // 将 pipline  作为上下文值传入 
    // 如果该名称的模板未实现(不存在), 则输出 T1 就相当于在基础模板页中定义了一个子模板占位符.
    <span style="color:#75715e">{{</span><span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;name&#34;</span> <span style="color:#a6e22e">pipeline</span><span style="color:#75715e">}}</span> T1 <span style="color:#75715e">{{</span><span style="color:#66d9ef">end</span><span style="color:#75715e">}}</span>
</code></pre></div>
    </div>

</div>

<p>这里再解析一下 with 关键字的用法，结合 Hugo 内置的 figure 说明，它的模板定义如下：</p>










<div class="code relative" id="demo.html"><div class="filename san-serif f6 dib lh-solid pl2 pv2">demo.html</div>
        <button class="needs-js copy bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button">
        </button>
        
    
    <div class="code-copy-content nt3" >
        <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go-html-template" data-lang="go-html-template">

    &lt;<span style="color:#f92672">figure</span><span style="color:#75715e">{{</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;class&#34;</span> <span style="color:#75715e">}}</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>&gt;
        <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;link&#34;</span> <span style="color:#75715e">-}}</span>
            &lt;<span style="color:#f92672">a</span> <span style="color:#a6e22e">href</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;link&#34;</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;target&#34;</span> <span style="color:#75715e">}}</span> <span style="color:#a6e22e">target</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;rel&#34;</span> <span style="color:#75715e">}}</span> <span style="color:#a6e22e">rel</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>&gt;
        <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        &lt;<span style="color:#f92672">img</span> <span style="color:#a6e22e">src</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;src&#34;</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span>
             <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">or</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;alt&#34;</span><span style="color:#f92672">)</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;caption&#34;</span><span style="color:#f92672">)</span> <span style="color:#75715e">}}</span>
             <span style="color:#a6e22e">alt</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;alt&#34;</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">else</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;caption&#34;</span> <span style="color:#f92672">|</span> <span style="color:#a6e22e">markdownify</span><span style="color:#f92672">|</span> <span style="color:#a6e22e">plainify</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span>
             <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">-}}</span>
             <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;width&#34;</span> <span style="color:#75715e">}}</span> <span style="color:#a6e22e">width</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">-}}</span>
             <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;height&#34;</span> <span style="color:#75715e">}}</span> <span style="color:#a6e22e">height</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">-}}</span>
        /&gt; <span style="color:#75715e">&lt;!-- Closing img tag --&gt;</span>
        <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;link&#34;</span> <span style="color:#75715e">}}</span>&lt;/<span style="color:#f92672">a</span>&gt;<span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">-}}</span>
        <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">or</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">or</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;title&#34;</span><span style="color:#f92672">)</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;caption&#34;</span><span style="color:#f92672">))</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;attr&#34;</span><span style="color:#f92672">)</span> <span style="color:#75715e">-}}</span>
            &lt;<span style="color:#f92672">figcaption</span>&gt;
                <span style="color:#75715e">{{</span> <span style="color:#66d9ef">with</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;title&#34;</span><span style="color:#f92672">)</span> <span style="color:#75715e">-}}</span>
                    &lt;<span style="color:#f92672">h4</span>&gt;<span style="color:#75715e">{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span>&lt;/<span style="color:#f92672">h4</span>&gt;
                <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">-}}</span>
                <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">or</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;caption&#34;</span><span style="color:#f92672">)</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;attr&#34;</span><span style="color:#f92672">)</span> <span style="color:#75715e">-}}</span>&lt;<span style="color:#f92672">p</span>&gt;
                    <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;caption&#34;</span> <span style="color:#f92672">|</span> <span style="color:#a6e22e">markdownify</span> <span style="color:#75715e">-}}</span>
                    <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;attrlink&#34;</span> <span style="color:#75715e">}}</span>
                        &lt;<span style="color:#f92672">a</span> <span style="color:#a6e22e">href</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span>&gt;
                    <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">-}}</span>
                    <span style="color:#75715e">{{-</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;attr&#34;</span> <span style="color:#f92672">|</span> <span style="color:#a6e22e">markdownify</span> <span style="color:#75715e">-}}</span>
                    <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">.Get</span> <span style="color:#e6db74">&#34;attrlink&#34;</span> <span style="color:#75715e">}}</span>&lt;/<span style="color:#f92672">a</span>&gt;<span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>&lt;/<span style="color:#f92672">p</span>&gt;
                <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
            &lt;/<span style="color:#f92672">figcaption</span>&gt;
        <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
    &lt;/<span style="color:#f92672">figure</span>&gt;
</code></pre></div>
    </div>

</div>

<p>在页面中使用时，如下，传入的参数就是命名的参数，那么在 figure 模板内部就可以使用 .Get 来获取这些参数，只需要给定参数名字：</p>
<pre><code>{{&amp;lt figure src=&quot;/assets/demo.png&quot; title=&quot;demo&quot; width=&quot;50%&quot;&gt;}}
</code></pre>
<p>如果，传入的是一个数组，那么可以使用数字表示要获取的元素序号：</p>
<pre><code>{{ $quality := default &quot;100&quot; (.Get 1) }}
</code></pre>
<p>模板中常用的数据类型有字典 dict 和切片 slice：</p>
<pre><code>{{ $style := resources.Get &quot;css/main.css&quot; | resources.PostCSS (dict &quot;config&quot; &quot;customPostCSS.js&quot; &quot;noMap&quot; true) }}
</code></pre>
<h2 id="base-template-页面骨架模板">Base Template 页面骨架模板</h2>
<p>这是最基础的模板，为所有页面定义 HTML 标签基本结构，主要是 <strong>head</strong>、<strong>body</strong> 和页面设计布局定义。</p>
<p>通过 block 关键字设置区块定义：</p>










<div class="code relative" id="demo.html"><div class="filename san-serif f6 dib lh-solid pl2 pv2">demo.html</div>
        <button class="needs-js copy bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button">
        </button>
        
    
    <div class="code-copy-content nt3" >
        <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go-html-template" data-lang="go-html-template">
    <span style="color:#75715e">&lt;!DOCTYPE html&gt;</span>
    &lt;<span style="color:#f92672">html</span>&gt;
      &lt;<span style="color:#f92672">head</span>&gt;
        &lt;<span style="color:#f92672">meta</span> <span style="color:#a6e22e">charset</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;utf-8&#34;</span>&gt;
        &lt;<span style="color:#f92672">title</span>&gt;<span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;title&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span>
          <span style="color:#75715e">&lt;!-- Blocks may include default content. --&gt;</span>
          <span style="color:#75715e">{{</span> <span style="color:#a6e22e">.Site.Title</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>&lt;/<span style="color:#f92672">title</span>&gt;
      &lt;/<span style="color:#f92672">head</span>&gt;
      &lt;<span style="color:#f92672">body</span>&gt;
        <span style="color:#75715e">&lt;!-- Code that all your templates share, like a header --&gt;</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;main&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span>
          <span style="color:#75715e">&lt;!-- The part of the page that begins to differ between templates --&gt;</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;footer&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span>
          <span style="color:#75715e">&lt;!-- More shared code, perhaps a footer but that can be overridden if need be in  --&gt;</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
      &lt;/<span style="color:#f92672">body</span>&gt;
    &lt;/<span style="color:#f92672">html</span>&gt;
</code></pre></div>
    </div>

</div>

<p>然后，在页面模板中覆盖这些区块，比如在列表中：</p>
<pre><code>{{ define &quot;main&quot; }}
  &lt;h1&gt;Posts&lt;/h1&gt;
  {{ range .Pages }}
    &lt;article&gt;
      &lt;h2&gt;{{ .Title }}&lt;/h2&gt;
      {{ .Content }}
    &lt;/article&gt;
  {{ end }}
{{ end }}
</code></pre>
<p>前面的 block &ldquo;title&rdquo; 提供了默认的 <strong>{{ .Site.Title }}</strong> 模板，所以后续没有其它模板定义覆盖这个区块也没有问题，默认显示标题。</p>
<p>参考 Ananke 主题的 baseof.html 的定义，可以看到模板中引用了 <strong>data</strong> 目录下的数据，即样式定义：</p>
<pre><code>{{ $stylesheet := .Site.Data.webpack_assets.app }}
{{ with $stylesheet.css }}
  &lt;link href=&quot;{{ relURL (printf &quot;%s%s&quot; &quot;dist/&quot; .) }}&quot; rel=&quot;stylesheet&quot;&gt;
{{ end }}
</code></pre>
<p>webpack_assets.json 数据文件内容：</p>
<pre><code>{
  &quot;app&quot;: {
    &quot;js&quot;: &quot;js/app.3fc0f988d21662902933.js&quot;,
    &quot;css&quot;: &quot;css/app.4fc0b62e4b82c997bb0041217cd6b979.css&quot;
  }
}
</code></pre>
<p>Ananke 使用的 CSS 样式库是 tachyons，这个库很像我刚开始做的 Web 开发的样式组织风格，特别熟悉的味道。样式表中按不同表现的 CSS 属性进行分类提供定义，使用时在 HTML 标签上写上相应的 CSS 样式那么就有对应的效果。比如说，样式表中定义了表格的各种样式类，其中 .collapse 用来塌陷边框，在 HTML 的 table 标签上使用它就使用表格获得相应的效果。</p>
<p>在站点配置文件 config.toml 中</p>
<pre><code>{{ block &quot;favicon&quot; . }}
  {{ partialCached &quot;site-favicon.html&quot; . }}
{{ end }}
</code></pre>
<p>模板文件内容：</p>










<div class="code relative" id="demo.html"><div class="filename san-serif f6 dib lh-solid pl2 pv2">demo.html</div>
        <button class="needs-js copy bg-accent-color-dark f6 absolute top-0 right-0 lh-solid hover-bg-primary-color-dark bn white ph3 pv2" title="Copy this code to your clipboard." data-clipboard-action="copy" aria-label="copy button">
        </button>
        
    
    <div class="code-copy-content nt3" >
        <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go-html-template" data-lang="go-html-template">

    <span style="color:#75715e">&lt;!DOCTYPE html&gt;</span>
    &lt;<span style="color:#f92672">html</span> <span style="color:#a6e22e">lang</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">$.Site.LanguageCode</span> <span style="color:#f92672">|</span> <span style="color:#a6e22e">default</span> <span style="color:#e6db74">&#34;en&#34;</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span>&gt;
      &lt;<span style="color:#f92672">head</span>&gt;
        &lt;<span style="color:#f92672">meta</span> <span style="color:#a6e22e">charset</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;utf-8&#34;</span>&gt;
        &lt;<span style="color:#f92672">meta</span> <span style="color:#a6e22e">http-equiv</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;X-UA-Compatible&#34;</span> <span style="color:#a6e22e">content</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;IE=edge,chrome=1&#34;</span>&gt;
        <span style="color:#75715e">{{</span><span style="color:#75715e">/* NOTE: the Site&#39;s title, and if there is a page title, that is set too */</span><span style="color:#75715e">}}</span>
        &lt;<span style="color:#f92672">title</span>&gt;<span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;title&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">.Params.Title</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span> | <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">.Site.Title</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>&lt;/<span style="color:#f92672">title</span>&gt;
        &lt;<span style="color:#f92672">meta</span> <span style="color:#a6e22e">name</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;viewport&#34;</span> <span style="color:#a6e22e">content</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;width=device-width,minimum-scale=1&#34;</span>&gt;
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">hugo</span><span style="color:#a6e22e">.Generator</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span><span style="color:#75715e">/* NOTE: For Production make sure you add `HUGO_ENV=&#34;production&#34;` before your build command */</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">eq</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">getenv</span> <span style="color:#e6db74">&#34;HUGO_ENV&#34;</span><span style="color:#f92672">)</span> <span style="color:#e6db74">&#34;production&#34;</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">or</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">eq</span> <span style="color:#a6e22e">.Site.Params.env</span> <span style="color:#e6db74">&#34;production&#34;</span><span style="color:#f92672">)</span>  <span style="color:#75715e">}}</span>
          &lt;<span style="color:#f92672">META</span> <span style="color:#a6e22e">NAME</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;ROBOTS&#34;</span> <span style="color:#a6e22e">CONTENT</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;INDEX, FOLLOW&#34;</span>&gt;
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">else</span> <span style="color:#75715e">}}</span>
          &lt;<span style="color:#f92672">META</span> <span style="color:#a6e22e">NAME</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;ROBOTS&#34;</span> <span style="color:#a6e22e">CONTENT</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;NOINDEX, NOFOLLOW&#34;</span>&gt;
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>

        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">$stylesheet</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">.Site.Data.webpack_assets.app</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">$stylesheet</span><span style="color:#a6e22e">.css</span> <span style="color:#75715e">}}</span>
          &lt;<span style="color:#f92672">link</span> <span style="color:#a6e22e">href</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">relURL</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">printf</span> <span style="color:#e6db74">&#34;%s%s&#34;</span> <span style="color:#e6db74">&#34;dist/&#34;</span> <span style="color:#a6e22e">.</span><span style="color:#f92672">)</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span> <span style="color:#a6e22e">rel</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;stylesheet&#34;</span>&gt;
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>

        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">.Site.Params.custom_css</span> <span style="color:#75715e">}}</span>
          &lt;<span style="color:#f92672">link</span> <span style="color:#a6e22e">rel</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;stylesheet&#34;</span> <span style="color:#a6e22e">href</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">relURL</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">.</span><span style="color:#f92672">)</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span>&gt;
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>

        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;favicon&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span>
          <span style="color:#75715e">{{</span> <span style="color:#a6e22e">partialCached</span> <span style="color:#e6db74">&#34;site-favicon.html&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>

        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">.OutputFormats.Get</span> <span style="color:#e6db74">&#34;RSS&#34;</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">.OutputFormats.Get</span> <span style="color:#e6db74">&#34;RSS&#34;</span> <span style="color:#75715e">}}</span>
          &lt;<span style="color:#f92672">link</span> <span style="color:#a6e22e">href</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.RelPermalink</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span> <span style="color:#a6e22e">rel</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;alternate&#34;</span> <span style="color:#a6e22e">type</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;application/rss+xml&#34;</span> <span style="color:#a6e22e">title</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">$.Site.Title</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span> /&gt;
          &lt;<span style="color:#f92672">link</span> <span style="color:#a6e22e">href</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.RelPermalink</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span> <span style="color:#a6e22e">rel</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;feed&#34;</span> <span style="color:#a6e22e">type</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;application/rss+xml&#34;</span> <span style="color:#a6e22e">title</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;</span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">$.Site.Title</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span> /&gt;
          <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        
        <span style="color:#75715e">{{</span><span style="color:#75715e">/* NOTE: These Hugo Internal Templates can be found starting at https://github.com/spf13/hugo/blob/master/tpl/tplimpl/template_embedded.go#L158 */</span><span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">template</span> <span style="color:#e6db74">&#34;_internal/opengraph.html&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">-}}</span>
        <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">template</span> <span style="color:#e6db74">&#34;_internal/schema.html&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">-}}</span>
        <span style="color:#75715e">{{-</span> <span style="color:#66d9ef">template</span> <span style="color:#e6db74">&#34;_internal/twitter_cards.html&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">-}}</span>

        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">eq</span> <span style="color:#f92672">(</span><span style="color:#a6e22e">getenv</span> <span style="color:#e6db74">&#34;HUGO_ENV&#34;</span><span style="color:#f92672">)</span> <span style="color:#e6db74">&#34;production&#34;</span> <span style="color:#f92672">|</span> <span style="color:#66d9ef">or</span> <span style="color:#f92672">(</span><span style="color:#66d9ef">eq</span> <span style="color:#a6e22e">.Site.Params.env</span> <span style="color:#e6db74">&#34;production&#34;</span><span style="color:#f92672">)</span>  <span style="color:#75715e">}}</span>
          <span style="color:#75715e">{{</span> <span style="color:#66d9ef">template</span> <span style="color:#e6db74">&#34;_internal/google_analytics_async.html&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;head&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">partial</span> <span style="color:#e6db74">&#34;head-additions.html&#34;</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
      &lt;/<span style="color:#f92672">head</span>&gt;

      &lt;<span style="color:#f92672">body</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;ma0 </span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">$.Param</span> <span style="color:#e6db74">&#34;body_classes&#34;</span>  <span style="color:#f92672">|</span> <span style="color:#a6e22e">default</span> <span style="color:#e6db74">&#34;avenir bg-near-white&#34;</span><span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">with</span> <span style="color:#a6e22e">getenv</span> <span style="color:#e6db74">&#34;HUGO_ENV&#34;</span> <span style="color:#75715e">}}</span><span style="color:#e6db74"> </span><span style="color:#75715e">{{</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span><span style="color:#e6db74">&#34;</span>&gt;

        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;header&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">partial</span> <span style="color:#e6db74">&#34;site-header.html&#34;</span> <span style="color:#a6e22e">.</span><span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        &lt;<span style="color:#f92672">main</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;pb7&#34;</span> <span style="color:#a6e22e">role</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;main&#34;</span>&gt;
          <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;main&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        &lt;/<span style="color:#f92672">main</span>&gt;
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;footer&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">partialCached</span> <span style="color:#e6db74">&#34;site-footer.html&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
        <span style="color:#75715e">{{</span> <span style="color:#a6e22e">block</span> <span style="color:#e6db74">&#34;scripts&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#a6e22e">partialCached</span> <span style="color:#e6db74">&#34;site-scripts.html&#34;</span> <span style="color:#a6e22e">.</span> <span style="color:#75715e">}}{{</span> <span style="color:#66d9ef">end</span> <span style="color:#75715e">}}</span>
      &lt;/<span style="color:#f92672">body</span>&gt;
    &lt;/<span style="color:#f92672">html</span>&gt;
</code></pre></div>
    </div>

</div>

 
      <a title="Prev Post" class="prev fl" href="https://jimbowhy.gitee.io/tutorials/ch06-multilingual/">VI: Multilingual 多语言支持</a>
      
       
      <a title="Next Post" class="next fr" href="https://jimbowhy.gitee.io/tutorials/ch05b-hugo-templates/">V: Templates 其它模板</a>
      
      <p class="cf"></p><ul class="pa0">
  
   <li class="list">
     <a href="/tags/hugo" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">hugo</a>
   </li>
  
   <li class="list">
     <a href="/tags/menu" class="link f5 grow no-underline br-pill ba ph3 pv2 mb2 dib black sans-serif">menu</a>
   </li>
  
</ul>
<div class="mt3 instapaper_ignoref">
      
      
      </div>
    </div>

    <aside class="w-30-l mt6-l">




  <div class="bg-light-gray pa3 nested-list-reset nested-copy-line-height nested-links">
    <p class="f5 b mb3">相关內容</p>
    <ul class="pa0 list">
	   
	     <li  class="mb2">
          <a href="/tutorials/ch05b-hugo-templates/">V: Templates 其它模板</a>
        </li>
	    
	     <li  class="mb2">
          <a href="/tutorials/ch07-varialbes/">VII: Variables 对象变量</a>
        </li>
	    
	     <li  class="mb2">
          <a href="/tutorials/ch02-project-directories/">II: Hugo 目录组织</a>
        </li>
	    
	     <li  class="mb2">
          <a href="/tutorials/ch09-menus/">IX: Menus 菜单组织</a>
        </li>
	    
	     <li  class="mb2">
          <a href="/tutorials/ch01-hugo-web-framework/">I: Hugo Web Framework</a>
        </li>
	    
	     <li  class="mb2">
          <a href="/tutorials/ch04-hugo-pipes/">IV: Hugo Pipes 管道处理</a>
        </li>
	    
	     <li  class="mb2">
          <a href="/tutorials/ch06-multilingual/">VI: Multilingual 多语言支持</a>
        </li>
	    
	     <li  class="mb2">
          <a href="/tutorials/ch03-hugo-modules/">III: Hugo Modules 模块</a>
        </li>
	    
	     <li  class="mb2">
          <a href="/tutorials/ch08-functions/">VIII: Functions 内置函数</a>
        </li>
	    
    </ul>
</div>

</aside>

  </article>

    </main>

    <footer class="bg-black bottom-0 w-100 pa3" role="contentinfo">
  <div class="flex justify-between">
  <a class="f4 fw4 hover-white no-underline white-70 dn dib-ns pv2 ph3" href="https://jimbowhy.gitee.io/" >
    &copy;  Nothing is Mine 2021 
  </a>
    <div>







<a href="https://www.linkedin.com/in/jimboyeah/?zh" target="_blank" class="link-transition linkedin link dib z-999 pt3 pt0-l mr1" title="LinkedIn link" rel="noopener" aria-label="follow on LinkedIn——Opens in a new window">
  <svg  height="32px"  style="enable-background:new 0 0 65 65;" version="1.1" viewBox="0 0 65 65" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <path d="M50.837,48.137V36.425c0-6.275-3.35-9.195-7.816-9.195  c-3.604,0-5.219,1.983-6.119,3.374V27.71h-6.79c0.09,1.917,0,20.427,0,20.427h6.79V36.729c0-0.609,0.044-1.219,0.224-1.655  c0.49-1.22,1.607-2.483,3.482-2.483c2.458,0,3.44,1.873,3.44,4.618v10.929H50.837z M22.959,24.922c2.367,0,3.842-1.57,3.842-3.531  c-0.044-2.003-1.475-3.528-3.797-3.528s-3.841,1.524-3.841,3.528c0,1.961,1.474,3.531,3.753,3.531H22.959z M34,64  C17.432,64,4,50.568,4,34C4,17.431,17.432,4,34,4s30,13.431,30,30C64,50.568,50.568,64,34,64z M26.354,48.137V27.71h-6.789v20.427  H26.354z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>

<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000" width="8px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>
</span></a>


<a href="https://github.com/jimboyeah/jimboyeah.github.io" target="_blank" class="link-transition github link dib z-999 pt3 pt0-l mr1" title="Github link" rel="noopener" aria-label="follow on Github——Opens in a new window">
  <svg  height="32px"  style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512" width="32px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
  <path d="M256,32C132.3,32,32,134.8,32,261.7c0,101.5,64.2,187.5,153.2,217.9c11.2,2.1,15.3-5,15.3-11.1   c0-5.5-0.2-19.9-0.3-39.1c-62.3,13.9-75.5-30.8-75.5-30.8c-10.2-26.5-24.9-33.6-24.9-33.6c-20.3-14.3,1.5-14,1.5-14   c22.5,1.6,34.3,23.7,34.3,23.7c20,35.1,52.4,25,65.2,19.1c2-14.8,7.8-25,14.2-30.7c-49.7-5.8-102-25.5-102-113.5   c0-25.1,8.7-45.6,23-61.6c-2.3-5.8-10-29.2,2.2-60.8c0,0,18.8-6.2,61.6,23.5c17.9-5.1,37-7.6,56.1-7.7c19,0.1,38.2,2.6,56.1,7.7   c42.8-29.7,61.5-23.5,61.5-23.5c12.2,31.6,4.5,55,2.2,60.8c14.3,16.1,23,36.6,23,61.6c0,88.2-52.4,107.6-102.3,113.3   c8,7.1,15.2,21.1,15.2,42.5c0,30.7-0.3,55.5-0.3,63c0,6.1,4,13.3,15.4,11C415.9,449.1,480,363.1,480,261.7   C480,134.8,379.7,32,256,32z"/>
</svg>

<span class="new-window"><svg  height="8px"  style="enable-background:new 0 0 1000 1000;" version="1.1" viewBox="0 0 1000 1000" width="8px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<path d="M598 128h298v298h-86v-152l-418 418-60-60 418-418h-152v-86zM810 810v-298h86v298c0 46-40 86-86 86h-596c-48 0-86-40-86-86v-596c0-46 38-86 86-86h298v86h-298v596h596z" style="fill-rule:evenodd;clip-rule:evenodd;fill:;"/>
</svg>
</span></a>







</div>
  </div>
</footer>

    

  <script src="/dist/js/app.3fc0f988d21662902933.js"></script>


  </body>
</html>
